package com.turing.common.util;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author Maven
 * @version V1.0
 * @Package com.turing.contrasts.utils
 * @date 2022/2/21 17:00
 * @Copyright © 2022 深圳市图灵机器人有限公司
 */
public class PdfBoxUtils {

    public static boolean hasImage(String path){

        try {
            PDDocument document = PDDocument.load(new File(path));

            for (int i = 0; i <= document.getNumberOfPages() && i <= 2; i++) {
                PDPage page=document.getPage(i);
                PDResources resources = page.getResources();
                Iterable<COSName> xobjects =resources.getXObjectNames();
                if(xobjects!=null) {
                    Iterator<COSName> imageIter = xobjects.iterator();
                    if (imageIter.hasNext()) {

                        System.out.println("image page no:" + i);
                        if (i == 2 || i == document.getNumberOfPages()) {
                            return true;
                        }
                    } else {
                        //前三页中，有一页没有图片就走pdf。
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return true;
        }

        return false;
    }


    public static void generatePictureFromPdf(String path) throws IOException {

        PDDocument document = PDDocument.load(new File(path));
        PDDocument documentNew = new PDDocument();

        int newPdfPage=0;
        Map<Integer,Integer> pageMap = new HashMap<>();
        for(int i=0;i<document.getNumberOfPages();i++){
            PDPage page=document.getPage(i);
            PDResources resources = page.getResources();
            Iterable<COSName> xobjects =resources.getXObjectNames();
            if(xobjects!=null) {
                Iterator<COSName> imageIter = xobjects.iterator();
                if(imageIter.hasNext()){
//                    PdfWriter.getInstance(documentNew,new FileOutputStream("src/poi/itext/HelloWorld.pdf"));
                    documentNew.addPage(page);
                    pageMap.put(i,newPdfPage++);
                }
                documentNew.save("C:\\Users\\zhiqiang\\Desktop\\项目\\产品化\\要素抽取\\新建文件夹\\111_testpdf.pdf");
                while (imageIter.hasNext()) {
                    COSName cosName = imageIter.next();
                    boolean isImageXObject = resources.isImageXObject(cosName);
                    if (isImageXObject) {
                        //获取每页资源的图片
                        PDImageXObject ixt = (PDImageXObject) resources.getXObject(cosName);
                        File outputfile = new File("C:\\Users\\zhiqiang\\Desktop\\项目\\产品化\\要素抽取\\新建文件夹\\111_" + cosName.getName() + ".jpg");
                        ImageIO.write(ixt.getImage(), "jpg", outputfile);//可保存图片到本地
                    }
                }
            }
        }

    }

    public static void main(String[] args) throws Exception {

//        System.out.println(hasImage("D:\\project\\申购确认单0520.pdf"));
        System.out.println(hasImage("C:\\Users\\zhiqiang\\Desktop\\项目\\产品化\\要素抽取\\新建文件夹\\外汇缴款通知单脱敏样例1.pdf"));

    }

}



